﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BizMock.Tools;
using System.Xml.Serialization;

namespace BizMock.Tests
{
    /// <summary>
    /// Summary description for artTests
    /// </summary>
    [TestClass]
    public class ArtifactsTests
    {

        private TestContext testContextInstance;
        private static Artifacts artifacts = new Artifacts();


        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) 
        {
            artifacts.Initialize(true);
        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize() 
        {
            BizMockery.Reset(true);
        }
        
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        [DeploymentItem("MessageInstances")]
        //testing an orchestration that receives a multipart message and return 2 multipart messages
        public void MultipartWithLoopDirectTest()
        {
            Expect.TIMEOUT = 6000;
            Expect.DELAY = 5000;
            artifacts.MultipartWithLoopOrchestration.Start();

            artifacts.Msg_MultiPart.InterchangeID = "X";
            //art.Vrf_Multipart.MultiPart1.Field = "Y";
            //art.Vrf_Multipart.MultiPart2.Field = "Y";            
            Submit.Request(artifacts.Msg_MultiPart).To(artifacts.OneWayDirect);
            Expect.AtLeast(2).Request.At(artifacts.OWPort_2).Verify(artifacts.Vrf_Multipart);
        }

        [TestMethod]
        [DeploymentItem("MessageInstances")]
        //testing an orchestration that receives a multipart message and return 2 multipart messages
        public void MultipartWithLoopTest()
        {
            Expect.TIMEOUT = 6000;
            Expect.DELAY = 5000;
            artifacts.MultipartWithLoopOrchestration.Start();

            artifacts.Msg_MultiPart.InterchangeID = "X";
            //art.Vrf_Multipart.MultiPart1.Field = "Y";
            //art.Vrf_Multipart.MultiPart2.Field = "Y";            
            Submit.Request(artifacts.Msg_MultiPart).To(artifacts.OWPort_1);
            Expect.AtLeast(2).Request.At(artifacts.OWPort_2).Verify(artifacts.Vrf_Multipart);

        }

        [TestMethod]
        [DeploymentItem("MessageInstances")]
        //testing an orchestratio with a request response and solicit response ports that logs a message in the event log
        public void TwoWayPortsTest()
        {
            Expect.TIMEOUT = 6000;
            Expect.DELAY = 5000;
            //This will automatically create the bizmock ports, configure the orchestration and start it.
            //as specified in the art xml definition file
            artifacts.TwoWayPortsOrchestration.Start();

            //this will submit the MSG_simple message to the orchestration port TWPort_1
            Submit.Request(artifacts.Msg_Simple).To(artifacts.TWPort_1);
            //this will wait for one message at port TWPort_2, and check it verifies the expectations set in Vrf_simple
            Expect.One.Request.At(artifacts.TWPort_2).Verify(artifacts.Vrf_Simple);

            //This will check the event log for a log with a specific regular expression
            artifacts.Vrf_Flat.Match("prueba bizmock");
            Expect.EventLogSource(artifacts.BizMock_EventLog).Verify(artifacts.Vrf_Flat);

            //This will submit a response to the orchestration2 solicit response port
            Submit.Response(artifacts.Msg_Simple2).To(artifacts.TWPort_2);
            Expect.One.Response.At(artifacts.TWPort_1).Verify(artifacts.Vrf_Simple2);

        }

        [TestMethod]
        [DeploymentItem("MessageInstances")]
        //testing an orchestration that invokes a web service using a dynamic port.
        public void ConsumeWebServiceTest()
        {
            Expect.TIMEOUT = 6000;
            Expect.DELAY = 5000;
            artifacts.ConsumeWebServiceOrchestration.Start();
            artifacts.Msg_ServiceRequest.InterchangeID = "X";
            Submit.Request(artifacts.Msg_ServiceRequest).To(artifacts.GetDataInput);
            Expect.One.Request.At(artifacts.GetDataService).Verify("Hello World");
            Submit.Response("Service1_response.xml").To(artifacts.GetDataService);
            Expect.One.Request.At(artifacts.GetDataOutput).Verify("Bye World");
            Expect.One.Request.At(artifacts.GetDataService).Verify("Hello World");
            Submit.Response("Service1_response.xml").To(artifacts.GetDataService);
            Expect.One.Request.At(artifacts.GetDataOutput).Verify("Bye World");

        }
    }
}